<template>
  <!--新增修改商机页面-->
  <div class="add">
    <!--左边基础资料-->
    <div class="leftcont">
      <el-scrollbar style="height: 100%" >
        <div class="return">
        <span>
          <router-link :to="{path:'/business'}"><i class="el-icon-back"></i>   返回</router-link>
        </span>
        </div>
        <div class="info">基础资料</div>
        <el-form  :model="business" ref="businssForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="商机编号:">
            <el-input  v-model="business.businessName" style="width:210px" :readonly="true"></el-input>
          </el-form-item>
          <el-form-item label="商机负责人:">
            <el-input v-model="business.budinessEmpid.empName" style="width:210px" :readonly="true" ></el-input>
          </el-form-item>
          <el-form-item label="客户名称:" prop="customerByCustomId.cusName">
            <el-input v-model="business.customerByCustomId.cusName" style="width:210px" @focus="isCus=true" :disabled="business.businesId>0" ></el-input>
          </el-form-item>
          <el-form-item label="商机阶段:" >
            <el-select v-model="business.speedInfoBySpeedId" style="width:210px" value-key="speedId"  :readonly="business.businesId==0">
              <el-option  v-for="item in speedList" :key="item.speedId"  :label="item.speedContext" :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="可能性:" prop="possibility">
            <el-input v-model="business.speedInfoBySpeedId.possibility" style="width:210px" readonly> </el-input>
          </el-form-item>
          <el-form-item label="联系人:" prop="culinkmanByCupId">
            <el-select v-model="business.culinkmanByCupId" style="width:210px" value-key="cupId">
              <el-option v-for="cu in culinkManList" :key="cu.cupId" :label="cu.cupName" :value="cu">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商机状态:" prop="opportunityStatus">
            <el-select     v-model="business.opportunityStatus" style="width:210px;" >
              <el-option v-for="status in businessStauts" :label="status.label" :value="status.value" :key="status.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="预期金额:" prop="expectedAmount">
            <el-input  v-model="business.expectedAmount" style="width:210px"  placeholder="0.00"  oninput="value=value.replace(/[^\d.]/g,'')" ></el-input>
          </el-form-item>
          <el-form-item label="预计签约时间:" >
            <el-date-picker v-model="business.estimatedSigningTime" :picker-options="pickerOptions"  type="date"  placeholder="选择日期"  format="yyyy-MM-dd" style="width:210px">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input type="textarea" :rows="2"  placeholder="请输入内容" v-model="business.remarks" style="width:210px" >
            </el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
    </div>
    <!--右边商品-->
    <div class="rightcont">
      <div class="tableInfo">
        <el-row>
          <el-col :span="16">
            <div class="info1">产品信息</div>
          </el-col>
          <el-col :span="5">
            <el-button type="text"   style="margin-top: 6px;margin-left: 25px;"  @click="lookHistoryProducts">查看历史产品</el-button>
          </el-col>
          <el-col :span="3">
            <el-button type="text"  @click="openAddProducts" style="margin-top: 6px;">添加产品</el-button>
          </el-col>
        </el-row>
        <el-table :data="detailsList"  style="width:98%;" max-height="300">
          <el-table-column fixed  prop="zproductByProductId.productName"  label="产品名称"  width="110" >
          </el-table-column>
          <el-table-column  prop="zproductByProductId.prModel"   label="型号"  width="120" align="center">
          </el-table-column>
          <el-table-column  label="售价" align="center">
            <template slot-scope="ss1">
              {{ss1.row.detPrice | formatMoney}}
            </template>
          </el-table-column>
          <el-table-column label="折扣%">
            <template slot-scope="z">
            <el-input size="small"  v-model="z.row.detDiscount"  @change="checkMoney(z.$index,z.row)" style="width: 60px" oninput="value=value.replace(/[^\d.]/g,'')" ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="折后价" prop="detDiscountedPrice">
            <template slot-scope="ss2">
              {{ss2.row.detDiscountedPrice | formatMoney}}
            </template>
          </el-table-column>
          <el-table-column  label="数量" width="160" align="center">
            <template slot-scope="s">
              <el-input-number v-model="s.row.detNum"  :min="1" :max="10" size="small" @change="smallPrice(s.$index,s.row)"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="单位"  prop="psUnit" align="center">
          </el-table-column>
          <el-table-column label="小计"   align="center">
            <template slot-scope="ss3">
              {{ss3.row.detTotal | formatMoney}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="130" align="center">
            <template slot-scope="d">
              <el-button type="text" size="small" icon="el-icon-delete" @click="deleteGood(d.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row style="margin-right: 20px;">
          <el-col>
            <p style="margin-top: 10px;font-size:16px;float: right">
              总金额:<span style="color:red;">￥{{total | formatMoney}}</span>
            </p>
          </el-col>
          <el-col>
            <p style="margin-top: 10px;float: right">
              <el-button type="primary" @click="saveBusinessInfo">确定</el-button>
            </p>
          </el-col>
        </el-row>
      </div>

    </div>
    <!--客户列表 isCus-->
    <el-dialog title="选择客户" :visible.sync="isCus">
      <el-row>
        <el-col>
          客户筛选:
          <el-input  placeholder="请输入客户名称" v-model="customerMc" @change="searchCustomer"  suffix-icon="el-icon-search" style="width:200px;">
          </el-input>
        </el-col>
        <el-col>
          <el-table  :data="customerList" height="250" style="width: 100%">
            <el-table-column width="55">
              <template slot-scope="scope">
                <el-radio v-model="radio" :label="scope.row.customId" @change="getCustomerRow(scope.row)">
                  {{''}}
                </el-radio>
              </template>
            </el-table-column>
            <el-table-column prop="cusName" label="客户名称" >
            </el-table-column>
            <el-table-column prop="cusSource" label="来源">
            </el-table-column>
            <el-table-column  label="创建时间">
              <template slot-scope="scope">
                {{scope.row.customTime | formatDate}}
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col>
          <el-pagination  @size-change="handleSizeChange1"  @current-change="changePage1" :current-page.sync="currentPage1"
                          :page-sizes="[20,40, 60,100]" :page-size="pageSize1" layout="total, sizes, prev, pager, next, jumper"
                          :total="totalSize1" style="float: right;margin-top: 10px;">
          </el-pagination>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isCus = false">取 消</el-button>
         <el-button type="primary" @click="customerOK">确 定</el-button>
      </span>
    </el-dialog>
    <!--添加产品-->
    <el-dialog title="产品列表" :visible.sync="isAdd" width="60%">
      <el-row>
        <el-col>
          产品筛选:<el-cascader v-model="category" ref="myCascader" :options="data2"
                            :props="{value:'pcId',children:'children', label: 'pcName',expandTrigger: 'hover' }"
                               @change="handleChange"  clearable></el-cascader>
          <el-input placeholder="请输入产品名称"  @change="searchProduct" suffix-icon="el-icon-search" v-model="searchPro"  style="width: 200px;margin-left:20px">
          </el-input>
        </el-col>
      </el-row>
      <el-row style="margin-top: 13px">
      <!--  {{showProducts()}}-->
       <!-- <el-col :span="3">
          已选产品:
        </el-col>
        <el-col :span="19" style="height:37px; margin-top: -7px;">
          <el-scrollbar style="height: 100%" >
            <el-tag v-for=" mi in multipleSelection"  type="in" :key="mi.prsId">
              {{mi.zproductByProductId.productName}}  {{mi.zproductByProductId.prModel}}
            </el-tag>
          </el-scrollbar>
        </el-col>-->
      </el-row>
        <el-row>
          <el-col>
            <el-table   :data="goodList.slice((currentPage2-1)*pageSize2,currentPage2*pageSize2)"
                        ref="multipleSelection"
                        style="width: 100%"  height="320">
              <el-table-column type="expand">
                <template slot-scope="props">
                  <el-form label-position="left" inline class="demo-table-expand">
                  <el-form-item  v-for="(pro,index) in props.row.zsidesmodelmiddlesByPsId"  :key="index" :label="pro.zspecificationsvalueBySpecId.zspecificationsByOnsId.onsName+':'">
                    <span>{{pro.zspecificationsvalueBySpecId.specVlaue}}</span>
                  </el-form-item>
                </el-form>
                </template>
              </el-table-column>
              <el-table-column type="selection" width="55"   >
              </el-table-column>
              <el-table-column  width="180" prop="zproductByProductId.productName" label="产品名称" >
              </el-table-column>
              <el-table-column width="180" prop="zproductByProductId.prModel" label="型号">
              </el-table-column>
              <el-table-column width="120"  label="图片">
                <template slot-scope="img">
                  <el-image
                    style="width:50px;height:50px;"
                    class="table-td-thumb"
                    :src="img.row.zproductByProductId.prImgpath"
                    :preview-src-list="[img.row.zproductByProductId.prImgpath]"
                  ></el-image>
                </template>
              </el-table-column>
              <el-table-column  label="售价">
                <template slot-scope="ss">
                  {{ss.row.psPrice | formatMoney}}
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <el-col>
            <el-pagination style="float:right;margin-top: 5px;" :current-page.sync="currentPage2"
                           :page-sizes="[10, 20, 30,40]"
                           :page-size="pageSize2" layout="total, sizes, prev, pager, next, jumper"
                           :total="goodList.length"
                           @current-change="changeProSize"
                           @size-change="handleSizeChange2">
            </el-pagination>
          </el-col>
        </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="determineProducts">确 定</el-button>
        <el-button @click="isAdd = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog  title="历史产品" :visible.sync="historyProducts" width="55%">
      <el-row>
        <el-col>
          搜索:
          <el-input suffix-icon="el-icon-search" v-model="searchHistoryProducts"
                    @change="changeSearchProducts" size="small" style="width: 30%;"></el-input>
        </el-col>
        <el-col>
          <el-table :data="historyProductsList.slice((currentPage-1)*pagesize,currentPage*pagesize)" style="width: 100%"   max-height="250">
              <el-table-column  label="日期" prop="date" width="150">
              </el-table-column>
              <el-table-column label="产品名称" prop="prName" fixed width="180">
              </el-table-column>
              <el-table-column prop="prModel"  width="120"   fixed
                               label="型号">
              </el-table-column>
              <el-table-column   width="150"
                               label="售价">
                <template slot-scope="p">
                  {{p.row.price | formatMoney}}
                </template>
              </el-table-column>
              <el-table-column prop="discount"  width="80"
                               label="折扣">
              </el-table-column>
              <el-table-column prop="discountedPrice"  width="150"
                               label="折后价">
                <template slot-scope="p1">
                  {{p1.row.discountedPrice | formatMoney}}
                </template>
              </el-table-column>
              <el-table-column label="采用">
                <template slot-scope="history">
                  <el-button type="primary" icon="el-icon-check" circle   @click="historyDiscount(history.row)"></el-button>
                </template>
              </el-table-column>
          </el-table>
        </el-col>
        <el-col>
          <el-pagination
            style="float:right;margin-top:7px;"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="historyProductsList.length">
          </el-pagination>
        </el-col>
        <el-col style="height:9px;"></el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
  import {formatTimeToStr} from "../assets/format";
  import TemplateVue from './homepage/TemplateVue'

  export default {
    components: {TemplateVue},
    data() {
      return {
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() < Date.now()- 8.64e7;
          }
        },
        businessStauts:[],//商机状态

        //选择客户弹框
        currentPage1:1,//当前页
        pageSize1:20,//页大小
        totalSize1:0,//总页数
        customerMc:'',//客户筛选输入框
        customerList:[],//客户列表
        isCus:false,
        radio:'',
        cusList:[],//确定客户
        cusDisable:false,

        isUpdate:false,//添加商品的显示与隐藏
        speedList:[],//进度数组
        culinkManList:[],//客户联系人数组

        business:{//商机对象
          businesId:'',//商机id
          businessName:'',//商机名称
          budinessEmpid:{//员工对象
            employeesId:0,//员工id
            empName:''//员工名称
          },//商机负责人
          customerByCustomId:{//客户对象
            customId:'',//客户id
            cusName:''//客户名称
          },//客户名称
          speedInfoBySpeedId:{//进度对象
            speedId:1,//进度id
            speedContext:'初级沟通',//进度内容
            possibility:'10%',//可能性
          },//商机进度

          culinkmanByCupId:{
            cupId:0,//客户联系人id
            cupName:''//客户联系人名称
          },//客户联系人
          expectedAmount:'',//预期金额
          estimatedSigningTime:'',//预计签约时间
          opportunityStatus:1,//商机状态
          remarks:''//备注
        },
        rules:{
          'customerByCustomId.cusName':[{required: true, message: '请选择客户', trigger: 'change'}],
         culinkmanByCupId:[{required: true, message: '请选择入联系人', trigger: 'change'}],
          expectedAmount:[
            {required: true, message: '预期金额不能为空!', trigger: 'blur'},
          ],
          opportunityStatus:[{required: true, message: '商机状态不能为空!', trigger: 'change'},]
        },

        in:'info',//标签类型
        searchPro:'',//添加商品里面的查询
        data2: [],//产品分类数组
        category:'',//类型查询
        currentPage2:1,//当前页
        pageSize2:10,//页大小
        totalSize2:0,//总页数
        goodList:[],//产品列表
        detailsList: [],//确定产品列表
        multipleSelection: [],//存取勾选中的产品
        currentPage4: 4,//分页
        category:'',
        pcId:0,//类型id
        pbId:0,//品牌id
        isAdd:false,
        flag:true,//控制折扣
        historyProducts:false, //客户历史产品
        historyProductsList:[],//客户历史产品集合
        searchHistoryProducts:'',//搜索历史产品名称
        num:0,//记录历史产品数组长度
        currentPage:1,
        pagesize:10,

      }

    },created :function(){
      this.business.budinessEmpid=JSON.parse(sessionStorage.getItem('user'));
      if(this.$refs.businssForm != undefined){
        this.$refs.businssForm.resetFields();
      }
      if(this.$route.query.businesId> 0 &&this.$route.query.businesId!=''){
        //查询信息
       this.queryOne(this.$route.query.businesId);
      }else{
        this.scBusinessName();
      }
      //加载客户列表
      this.inDateCustomer(
        this.currentPage1,
        this.pageSize1,
        this.customerMc,
        JSON.parse(sessionStorage.getItem('user')).employeesId);
      //加载产品分类
      this.$axios.get("http://localhost:8088/buInfo/category")
        .then(v=>{
          this.data2=v.data;
        }).catch();
    },
    methods:{
      //修改前的查询
        queryOne(businesId){
          this.$axios.get("http://localhost:8088/buInfo/queryBusiness",
            {params:{
                    businesId:businesId
                  }}).then(v=>{
              //赋值给表单
              this.business=v.data;
              this.historyProductsData(v.data.customerByCustomId.customId,this.searchHistoryProducts);//历史产品
              //重新查询客户联系人
              this.$axios.get("http://localhost:8088/buInfo/culinkman",
                {params:{customerId: this.business.customerByCustomId.customId}})
                .then(v=>{
                  this.culinkManList=v.data;
                }).catch()
              this.business.culinkmanByCupId=v.data.culinkmanByCupId;
            }).catch()
          this.$axios.get("http://localhost:8088/buInfo/businessDatails",{params:{businesId:businesId}})
            .then(v=>{
              //赋值给产品数组
              this.detailsList =v.data;
            }).catch();
          this.speedList=[];
          this.businessStauts = [];
          this.speedList.push({speedId:1,speedContext:'初级沟通',possibility:'10%'});
          this.speedList.push({speedId:2,speedContext:'深度沟通',possibility:'30%'});
          this.speedList.push({speedId:3,speedContext:'方案设计',possibility:'60%'});
          this.speedList.push({speedId:4,speedContext:'报价设计',possibility:'80%'});
          this.speedList.push({speedId:5,speedContext:'签单',possibility:'100%'});
          this.businessStauts.push({label:'跟踪', value:1});
          this.businessStauts.push({label:'搁置',value:2});
          this.businessStauts.push({label:'成功',value:3});
          this.businessStauts.push({label:'失败',value:4},);
        },
      //生成商机名称
        scBusinessName(){
          //生成商机名称
          this.$axios.get("http://localhost:8088/buInfo/business_name")
            .then(v=>{
              this.business.businessName=v.data;
              this.speedList=[];
              this.businessStauts = [];
              this.speedList.push({speedId:1,speedContext:'初级沟通',possibility:'10%'});
              this.businessStauts.push({label:'跟踪', value:1});
            }).catch();
        },
      //清空表单
      clearForm(){
        this.business={//商机对象
            businesId:'',//商机id
            businessName:'',//商机名称
            budinessEmpid:{//员工对象
            employeesId:'',//员工id
              empName:''//员工名称
            },//商机负责人
          customerByCustomId:{//客户对象
            customId:0,//客户id
            cusName:''//客户名称
            },//客户名称
          speedInfoBySpeedId:{//进度对象
            speedId:1,//进度id
              speedContext:'初级沟通',//进度内容
            possibility:'10%',//可能性
          },//商机进度

            culinkmanByCupId:{
            cupId:0,//客户联系人id
              cupName:''//客户联系人名称
          },//客户联系人
          expectedAmount:'',//预期金额

            estimatedSigningTime:'',//预计签约时间
          opportunityStatus:1,//商机状态
            remarks:''//备注
        }
        this.detailsList=[];
      },
      //客户分页查询
      inDateCustomer(currentPage1,pageSize1,customerMc,employeesId){
        this.$axios.get("http://localhost:8088/buInfo/customer",
          {params:{page:currentPage1,size:pageSize1,cusName:customerMc,employeesId:employeesId}})
          .then(v=>{
            this.customerList=v.data.rows;
            this.totalSize1 =v.data.total;
          }).catch()

      },

      //客户名称模糊查询
      searchCustomer(){
        this.inDateCustomer(1,this.pageSize1,this.customerMc,JSON.parse(sessionStorage.getItem('user')).employeesId);
      },
      //改变客户分页的页码
      changePage1(val){
        this.inDateCustomer(val,this.pageSize1,this.customerMc,JSON.parse(sessionStorage.getItem('user')).employeesId);
      },
      //改变客户分页的每页页数
      handleSizeChange1(val){
        this.inDateCustomer(1,val,this.customerMc,JSON.parse(sessionStorage.getItem('user')).employeesId);
      },
      //单选框选中的行
      getCustomerRow(val){
        this.cusList=[];//先清空
        this.cusList.push(val);
      },
      //确定客户
      customerOK(){
        this.business.customerByCustomId=this.cusList[0];
        this.historyProductsData(this.business.customerByCustomId.customId,this.searchHistoryProducts);
        //进行查询对应联系人
        this.$axios.get("http://localhost:8088/buInfo/culinkman",
          {params:{customerId: this.business.customerByCustomId.customId}})
          .then(v=>{
            this.culinkManList=v.data;
            this.business.culinkmanByCupId=null;//清空联系人
            v.data.forEach((item,i)=>{
              if(item.cupMaster == 1){//为1该联系人为客户的首要联系人
                //默认为首要联系人
                this.business.culinkmanByCupId=item;
              }
            })
          }).catch()
        this.isCus=false;//关闭弹框
      },
      openAddProducts(){
        this.initProduct(this.searchPro,this.pcId,this.pbId);
        this.isAdd=true;
      },
      //产品列表
      initProduct(searchPro,pcId,pbId){
         this.$axios.get("http://localhost:8088/buInfo/newproduct_info",{
             params:{
               productName:searchPro,
               pcId:pcId,
               pbId:pbId
             }
         }).then(v=>{
             this.goodList=v.data;
         }).catch();
      },//改变产品列表页数
      //前端分页
      changeProSize: function (size) {
        this.currentPage2 = size;
      },
      handleSizeChange2: function(currentPage){
        this.pageSize2 = currentPage;

      },



      //类型加品牌查询 和模糊查询
      handleChange(item) {
        if(this.category !='' ){
          this.pbId=item[0];
          this.pcId=item[1];

        }else{
          this.pbId=0;
          this.pcId=0;
        }
        this.initProduct(this.searchPro,this.pcId,this.pbId);

      },
      searchProduct(){
        this.initProduct(this.searchPro,this.pcId,this.pbId);
      },
      //确认选择产品
      determineProducts(){
        var flag = false;
        //数组临时添加 数量:detNum 小计:detTotal 折扣:detDiscount
          this.$refs.multipleSelection.selection.forEach((item,i)=>{
            //判断已有的产品是否存在 当detailsList长度不为0的时候 可进行比较
            //detailsList长度为0的时候 可直接添加产品
            if(this.detailsList.length !=0){
              this.detailsList.forEach(list=>{
                //产品已存在，则提示产品已存在，只能修改产品数量
                if(item.psId == list.prsId){
                  this.$confirm('此产品已经存在，如需添加请直接修改数量!', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                  }).then(() => {
                  }).catch(() => {
                  });
                  flag=true;
                }

              })
            }
            if(flag==false){
              //不存在产品,则可以添加
              this.addList(item,i);
            }
          })
        this.multipleSelection=[];
        this.$refs.multipleSelection.clearSelection();
        //关闭弹框
        this.isAdd=false;
      },
      //添加的产品的方法
      addList(item,i){
        this.$set(item,'prsId',item.psId);
        this.$set(item,'detPrice',item.psPrice);
        this.$set(item,'detDiscount','0.00');
        this.$set(item,'detDiscountedPrice',item.psPrice);
        this.$set(item,'detNum',1);
        this.$set(item,'detTotal',item.psPrice);
        this.$set(item,'detFactoryrice',item.psFactoryPrice);
        this.detailsList.splice(i,0,item);
      },

      //验证折扣是否正确
      checkMoney(index,row){
        this.flag=true;
        var a=/^0+\.\d{2}$/;
        if(row.detDiscount==''){
          this.detailsList[index].detDiscount='0.00';//重新赋值
          this.detailsList[index].detDiscountedPrice=parseFloat(row.detPrice).toFixed(2);
          this.smallPrice(index, this.detailsList[index]);
        }else{
          if(a.test(row.detDiscount)){
            this.smallPrice(index,row);
          }else{
            this.$message.error("折扣格式错误!");
            this.flag=false;
          }
        }
      },
      //产品计算小计
      smallPrice(index,row){
        //小计不能低于成本价
        let price=0;//小计
        let jprice=0;//进价
        let detDiscountedPrice=0;
        if(row.detDiscount=='0.00'){//不享受折扣
          //售价*数量
          price=parseFloat(row.detPrice).toFixed(2)*row.detNum;
          detDiscountedPrice = parseFloat(row.detPrice);
          this.flag=true;
        }else{//享受折扣
          //售价*折扣*数量
          price=parseFloat(row.detPrice).toFixed(2)*parseFloat(row.detDiscount).toFixed(2)*row.detNum;
          jprice=parseFloat(row.detFactoryrice).toFixed(2)*row.detNum;
          detDiscountedPrice=parseFloat(row.detPrice).toFixed(2)*parseFloat(row.detDiscount).toFixed(2);
          if(price<jprice){
            this.$message.error("折扣不能太低！");
            this.flag = false;
          }else{
            this.flag = true;
          }
        }
        if(this.flag==true){
          this.detailsList[index].detDiscountedPrice=detDiscountedPrice;//
          this.detailsList[index].detTotal=price;//重新给小计赋值
        }
      },
      //查看客户的历史产品
      lookHistoryProducts(searchHistoryProducts){
        if(this.business.customerByCustomId.customId==0){
          this.$message({
            type: 'error',
            message: '请选择客户！'
          });
        }else{
          if(this.num == 0){
            this.$message({
              type: 'info',
              message: '该客户没有历史产品！'
            });
          }else{
            this.historyProducts = true;//打开弹框
          }

        }

      },
      //查询出的历史产品数据
      historyProductsData(customId,searchHistoryProducts){
        this.$axios.get("http://localhost:8088/buDetails/select_histroy_products", {
          params:{
            customId:customId,
            prName:searchHistoryProducts
          }
        }).then(v=>{
          this.historyProductsList = [];
          this.historyProductsList = v.data;
          this.num = this.historyProductsList.length;
        }).catch();
      },
      //模糊查询
      changeSearchProducts(){
        this.historyProductsData(this.business.customerByCustomId.customId,this.searchHistoryProducts);
      },
      handleSizeChange: function(val){
        this.pageSize = val;
      },
      handleCurrentChange:function(val){
          this.currentPage = val;
      },
      //采用历史折扣
      historyDiscount(row){
        this.$confirm('此操作将采用'+row.prName+row.prModel+'的折扣'+row.discount+', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if(this.detailsList.length==0){
            this.$message({
              type: 'error',
              message: '采用失败！'
            });
          }else{
            var flag=false;
            this.detailsList.forEach((item,i)=>{
              if(item.prsId == row.prsId){
                this.detailsList[i].detDiscount = row.discount;
                this.smallPrice(i, this.detailsList[i]);//重新计算价格
                flag=true;
              }
            })
            this.historyProducts = false;
          }
          if(flag==false){
            this.$message({
              type: 'error',
              message: '相同产品才能采用历史折扣！'
            });
          }else{
            this.historyProducts = false;
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      //删除产品
      deleteGood(row){
        this.$confirm('此操作将永久删除该产品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.detailsList.forEach((item,i) =>{
            if(item.prsId==row.prsId){
              this.detailsList.splice(i,1);
            }
          })
            this.$message({
              type: 'success',
              message: '删除成功!'
            });

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      //新增或修改商机
      saveBusinessInfo(){
          if( this.flag==true){
            this.$refs.businssForm.validate((valid) => {
              if (valid) {
                this.$axios.post("http://localhost:8088/buInfo/saveBusinessInfo",{
                  businesId:this.business.businesId,
                  businessName:this.business.businessName,
                  budinessEmpid:this.business.budinessEmpid,
                  customerByCustomId:this.business.customerByCustomId,
                  speedInfoBySpeedId:this.business.speedInfoBySpeedId,
                  culinkmanByCupId:this.business.culinkmanByCupId,
                  expectedAmount:this.business.expectedAmount,
                  estimatedSigningTime:this.business.estimatedSigningTime,
                  opportunityStatus:this.business.opportunityStatus,
                  remarks:this.business.remarks,
                  detailsByBusinesId:this.detailsList
                }).then(v=>{
                  this.$refs.businssForm.resetFields();
                  //清空表单
                  this.clearForm();
                  //接收后台传来的商机编号id
                  //把商机id传入商机信息页面
                  this.$router.push({
                    path:'/businessdetails',
                    query:{
                      businesId:v.data
                    }
                  })

                }).catch();
              } else {
                return false;
              }
            });
          }
      }
    },
    watch:{
      $route(){//跳转到该页面后需要进行的操作
        this.clearForm();
        if(this.$route.query.businesId> 0 &&this.$route.query.businesId!=''){
          this.cusDisable=true;
          //查询信息
          this.queryOne(this.$route.query.businesId);
        }else{
          this.scBusinessName();
        }
      }
    },
    computed:{
      //计算产品总金额
      total(){
        let tatalMoney=0;
        this.detailsList.forEach((item,i)=>{
          tatalMoney=tatalMoney+item.detTotal;
        })
        return tatalMoney;
      },
      //用于显示已选的产品

    }, filters: {
      formatDate: function (time) {
        if (time != null && time != "") {
          var date = new Date(time);
          return formatTimeToStr(date, "yyyy-MM-dd");
        } else {
          return "";
        }
      },
      formatMoney(val) {
        val = val.toString().replace(/\$|\,/g, "");
        if (isNaN(val)) {
          val = "0";
        }
        let sign = (val == (val = Math.abs(val)));
        val = Math.floor(val * 100 + 0.50000000001);
        let cents = val % 100;
        val = Math.floor(val / 100).toString();
        if (cents < 10) {
          cents = "0" + cents;
        }
        for (let i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
          val = val.substring(0, val.length - (4 * i + 3)) + "," + val.substring(val.length - (4 * i + 3));
        }
        return (((sign) ? "" : "-") + val + "." + cents);
      }
    }

  }

</script>

<style scoped>
a{
  text-decoration: none;
  color: #777;
}
  .add{
    border:1px solid #ccc;
    width:100%;
    background: white;
    border-radius: 5px;
    box-shadow:#ccc 0px 0px 10px;
    overflow:hidden;
  }
  .leftcont{
    width:46%;
    height: 550px;
    border-right:1px dashed #ccc;
    float: left;

  }
  .leftcont .el-form{
    margin-top: 8px;
    padding-left: 19%;
  }
  .rightcont{
    width:50%;
    border:0px solid red;
    float: left;
  }
  .info{
    color:#777;
    font-size:14px;
    border-left: 6px solid #409eff;
    width:80px;
    height:24px;
    text-align: center;
    margin-top: 15px;
    margin-left: 30px;

  }
  .info1{
    color:#777;
    font-size:14px;
    border-left: 6px solid #409eff;
    width:80px;
    height:24px;
    text-align: center;
    margin-top: 15px;
  }
  .add .return{
    border-right:1px solid #ccc;
    width:70px;
    color:#777;
    font-size: 13px;
    margin-left:10px;
    margin-top:10px;
    cursor: pointer;

  }
  .add .return:hover{
    color:#409eff;
  }
  .tableInfo {
    width: 100%;
    margin-left: 30px;
    margin-top: 22px;
  }
  >>> .el-input-number--small{
    width:98px;
  }
  .el-tag {
    margin-left:10px;
    margin-top:5px;
  }

  >>> .el-dialog__body{
    padding: 0px 20px;
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

</style>
